package com.lzx.dormsys.service.impl;

import com.github.pagehelper.PageInfo;
import com.lzx.dormsys.bean.RoleGroupBean;
import com.lzx.dormsys.bean.RoleGroupBeanExample;
import com.lzx.dormsys.bean.RoleGroupMenusBeanExample;
import com.lzx.dormsys.bean.RoleGroupMenusBeanKey;
import com.lzx.dormsys.entity.GroupRequest;
import com.lzx.dormsys.mapper.RoleGroupBeanMapper;
import com.lzx.dormsys.mapper.RoleGroupMenusBeanMapper;
import com.lzx.dormsys.service.RoleGroupBeanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class RoleGroupBeanServiceImpl implements RoleGroupBeanService {
    @Autowired
    RoleGroupBeanMapper roleGroupBeanMapper;

    @Autowired
    RoleGroupMenusBeanMapper roleGroupMenusBeanMapper;

    @Override
    public int insert(RoleGroupBean roleGroupBean) {
        return roleGroupBeanMapper.insert(roleGroupBean);
    }

    @Override
    public int update(RoleGroupBean roleGroupBean) {
        return 0;
    }

    @Override
    public List<RoleGroupBean> queryData(RoleGroupBean roleGroupBean) {
        return null;
    }

    @Override
    public PageInfo<RoleGroupBean> queryData(RoleGroupBean roleGroupBean, int pageSize, int pageNum) {
        List<RoleGroupBean> roleGroupBeans = roleGroupBeanMapper.selectByExample(null);
        PageInfo<RoleGroupBean> pageInfo = new PageInfo<>();
        pageInfo.setPageSize(pageSize);
        pageInfo.setPageNum(pageNum);
        pageInfo.setList(roleGroupBeans);
        return pageInfo;
    }

    @Override
    public int deleteByPrimaryKey(RoleGroupBean roleGroupBean) {
        return 0;
    }

    @Override
    public int deleteByPrimaryKey(String id) {
        RoleGroupMenusBeanExample example = new RoleGroupMenusBeanExample();
        RoleGroupMenusBeanExample.Criteria criteria =  example.createCriteria();
        criteria.andGroupidEqualTo(id);
        roleGroupMenusBeanMapper.deleteByExample(example);
        return roleGroupBeanMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void updateRoleGroup(GroupRequest groupRequest) {
        //1.删除 tbl_rolegroup_menus表中与groupid匹配的记录
        RoleGroupMenusBeanExample example = new RoleGroupMenusBeanExample();
        RoleGroupMenusBeanExample.Criteria criteria =  example.createCriteria();
        criteria.andGroupidEqualTo(groupRequest.getGroupid());
        roleGroupMenusBeanMapper.deleteByExample(example);

        //2 用split方法将逗号分隔的 menuids 形成数组  遍历 数组并往 表中写入数据
        String[] menuidArray  =  groupRequest.getMenuids().split(",");
        for(String menuid:menuidArray){
            RoleGroupMenusBeanKey key = new RoleGroupMenusBeanKey();
            key.setGroupid(groupRequest.getGroupid());
            key.setMenuid(menuid);
            roleGroupMenusBeanMapper.insert(key);
        }

        //3 修改rolegroup信息
        RoleGroupBean roleGroupBean = new RoleGroupBean();
        // 赋值
        roleGroupBean.setGroupid(groupRequest.getGroupid());
        roleGroupBean.setGroupname(groupRequest.getGroupname());
        roleGroupBean.setDescription(groupRequest.getDescription());

        roleGroupBeanMapper.updateByPrimaryKey(roleGroupBean);
    }


}
